package cn.neusoft.yuejian307.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.neusoft.yuejian307.R;
import cn.neusoft.yuejian307.bean.User;
import cn.neusoft.yuejian307.listener.UserListener;
import cn.neusoft.yuejian307.model.UserModel;

public class Register extends AppCompatActivity implements UserListener<User>{
    MyTitle myTitle;
    EditText phone,username,password,passwordtoo;
    String str1,str2,str3,str4;
    Button commit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        myTitle = (MyTitle)findViewById(R.id.tt);
        myTitle.setTitle("注册");
        init();
    }
    private void init(){
        commit =(Button)findViewById(R.id.commit);
        phone = (EditText)findViewById(R.id.phone);
        username = (EditText)findViewById(R.id.name);
        password = (EditText)findViewById(R.id.password);
        passwordtoo = (EditText)findViewById(R.id.passwordtoo);
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reg();
            }
        });
    }

    private void reg(){
        str1 = phone.getText().toString();
        str2 = username.getText().toString();
        str3 = password.getText().toString();
        str4 = passwordtoo.getText().toString();
        if (str1.length()>11){
            display("请输入正确的手机号");
        }else if(!str3.equals(str4)){
            display("密码两次输入不一样");
        }else{
            UserModel userModel = new UserModel();
            userModel.registerData(this,str1,str2,str4);
        }
    }

    private void display(String str){
        Toast.makeText(this,str,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onResponse(User user) {
        if (user.getSuccess()==1){
            display("登录成功");
            Intent intent = new Intent(this,LoginActivity.class);
            startActivity(intent);
            finish();
        }

    }

    @Override
    public void onFail(String msg) {
        display("访问服务器失败");
    }
}
